﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace CAS_ManagementSystem
{
    public partial class ViewLogin : Form
    {
        // 数据库连接字符串 - 根据实际情况修改
        private string connectionString = "Server=your_server;Database=your_database;User Id=your_username;Password=your_password;";

        public ViewLogin()
        {
            InitializeComponent();
        }

        private void login_Click(object sender, EventArgs e)
        {
            string username = userName.Text.Trim();
            string InputPassword = password.Text.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(InputPassword))
            {
                MessageBox.Show("账号和密码不能为空");
                return;
            }

            // 确定选择的角色
            int roleType = 0;
            if (radioButton1.Checked) roleType = 1; // 社团
            else if (radioButton2.Checked) roleType = 2; // 学生会
            else if (radioButton3.Checked) roleType = 3; // 校领导
            else if (radioButton4.Checked) roleType = 4; // 外联处
            else if (radioButton5.Checked) roleType = 5; // 企业
            else
            {
                MessageBox.Show("请选择登录角色");
                return;
            }

            // 验证用户登录
            if (ValidateUser(username, InputPassword, roleType))
            {
                // 登录成功，打开对应角色的主界面
                OpenRoleDashboard(roleType, username);
            }
            else
            {
                MessageBox.Show("登录失败，请检查账号、密码或角色选择");
            }
        }

        private bool ValidateUser(string username, string password, int roleType)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();

                    // 注意：实际应用中密码应该加密存储，这里简化处理
                    string query = "SELECT UserID FROM Users WHERE Username = @Username AND Password = @Password AND RoleType = @RoleType";

                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@Username", username);
                        command.Parameters.AddWithValue("@Password", password);
                        command.Parameters.AddWithValue("@RoleType", roleType);

                        object result = command.ExecuteScalar();
                        return result != null;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据库连接错误: " + ex.Message);
                    return false;
                }
            }
        }

        private void OpenRoleDashboard(int roleType, string username)
        {
            Form roleForm = null;

            switch (roleType)
            {
                case 1: // 社团
                    roleForm = new ClubDashboard(username);
                    break;
                case 2: // 学生会
                    roleForm = new StudentUnionDashboard(username);
                    break;
                case 3: // 校领导
                    roleForm = new SchoolLeaderDashboard(username);
                    break;
                case 4: // 外联处
                    roleForm = new ExternalAffairsDashboard(username);
                    break;
                case 5: // 企业
                    roleForm = new EnterpriseDashboard(username);
                    break;
            }

            if (roleForm != null)
            {
                this.Hide();
                roleForm.ShowDialog();
                this.Show();
            }
        }

        // 其他事件处理方法保持不变
        private void label2_Click(object sender, EventArgs e) { }
        private void userName_TextChanged(object sender, EventArgs e) { }
        private void password_TextChanged(object sender, EventArgs e) { }
        private void radioButton1_CheckedChanged(object sender, EventArgs e) { }
        private void radioButton2_CheckedChanged(object sender, EventArgs e) { }
        private void radioButton3_CheckedChanged(object sender, EventArgs e) { }
        private void radioButton4_CheckedChanged(object sender, EventArgs e) { }
        private void radioButton5_CheckedChanged(object sender, EventArgs e) { }
    }
}